﻿'Copyright (c) 2009 Grailtek

'Permission is hereby granted, free of charge, to any person
'obtaining a copy of this software and associated documentation
'files (the "Software"), to deal in the Software without
'restriction, including without limitation the rights to use,
'copy, modify, merge, publish, distribute, sublicense, and/or sell
'copies of the Software, and to permit persons to whom the
'Software is furnished to do so, subject to the following
'conditions:

'The above copyright notice and this permission notice shall be
'included in all copies or substantial portions of the Software.

'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
'EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
'OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
'NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
'HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
'WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
'FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
'OTHER DEALINGS IN THE SOFTWARE.

Imports System.Reflection

Namespace Aspect

    '<DebuggerStepThrough()> _
    <CLSCompliant(True)> _
    Public Class OutputMapping
        Public Sub New(ByVal iFieldAttribute As FieldAttribute, ByVal iField As FieldInfo)
            FieldAttribute = iFieldAttribute
            Field = iField
            Name = iField.Name
            FieldType = iField.FieldType
        End Sub
        Public Sub New(ByVal iFieldAttribute As FieldAttribute, ByVal iProperty As PropertyInfo)
            FieldAttribute = iFieldAttribute
            [Property] = iProperty
            Name = iProperty.Name
            FieldType = iProperty.PropertyType
        End Sub
        Public ReadOnly FieldAttribute As FieldAttribute
        Private ReadOnly Field As FieldInfo, [Property] As PropertyInfo
        Public ReadOnly Name As String
        Public ReadOnly FieldType As Type
        Public Function GetValue(ByVal o As Object) As Object
            If Field Is Nothing Then
                Return [Property].GetValue(o, Nothing)
            Else
                Return Field.GetValue(o)
            End If
        End Function
        Public Sub SetValue(ByVal o As Object, ByVal val As Object)
            Try
                If IsDBNull(o) Then o = Nothing
                If Field Is Nothing Then
                    'TODO: build direct IL set into dynamically-generated implementors
                    If [Property].CanWrite Then [Property].SetValue(o, val, Nothing)
                Else
                    Field.SetValue(o, val)
                End If
            Catch ex As ArgumentException
                Dim msg As String = String.Format("Field ""{0}"" expected data type {1}, but got value of type {2}.", Me.FieldAttribute.RegisterName, Me.FieldType.Name, If(val Is Nothing, "(null)", val.GetType.Name))
                Throw New ArgumentException(msg, ex)
            End Try
        End Sub
        Public Shared Function GetMappings(ByVal type As Type) As OutputMapping()
            Dim ret As New List(Of OutputMapping)
            For Each f As FieldInfo In type.GetFields(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.NonPublic)
                Dim op As FieldAttribute = f.GetCustomAttributes(True).OfType(Of FieldAttribute).FirstOrDefault
                If Not op Is Nothing Then ret.Add(New OutputMapping(op, f))
            Next
            For Each p As PropertyInfo In GetProperties(type)
                Dim op As FieldAttribute = p.GetCustomAttributes(True).OfType(Of FieldAttribute).FirstOrDefault
                If Not op Is Nothing Then ret.Add(New OutputMapping(op, p))
            Next
            Return ret.ToArray
        End Function
    End Class

End Namespace